﻿using System.Collections.Generic;
using UnityEngine;
/*测试GameObject*/
public class Test_GameObject1 : MonoBehaviour
{
    public GameObject prefab;
    private void Update1()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //实例化一个go物体
            GameObject go = Instantiate(prefab , Vector3.zero,Quaternion.identity );//物体，位置，旋转
            //后续对go物体进行操作
            Rigidbody rig = go.AddComponent<Rigidbody>();//添加刚体组件
            //Destroy(rig);
            //Destroy(go);
            Destroy(go,1);//延迟销毁
        }
    }
    //找到游戏物体
    private void Start1()
    {
        //根据名称找
        //GameObject go = GameObject.Find("Plane");
        //根据标签找
        GameObject[] go = GameObject.FindGameObjectsWithTag("a");
        foreach (var item in go)
        {
            item.GetComponent<MeshRenderer>().material.color = Color.red;
        }
    }
    //根据类型找组件,必须是全局唯一的
    Camera camera;

    private void Start2()
    {
        camera = FindObjectOfType<Camera>();
    }
    private void Update2()
    {
        if (Input.GetMouseButtonDown(1))
        {
            camera.fieldOfView = camera.fieldOfView == 60 ? 30 : 60;
        }
    }
    private void Update()
    {
        Transform[] array = FindObjectsOfType<Transform>();
        for (int i = 0; i < array.Length; i++)
        {
            Destroy(array[i].gameObject);
        }
    }
}
public class Test_GameObject : MonoBehaviour 
{
    Camera cam;
    GameObject lightGo;
    private void Start()
    {
        cam = this.gameObject.GetComponent<Camera>();
        lightGo = GameObject.Find("Directional Light");
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
           Test_Input t= this.gameObject.AddComponent<Test_Input>();//添加组件会返回组件对象
        }
        if (Input.GetMouseButtonDown(0))
        {
            lightGo.SetActive(false);
        }
        if (Input.GetMouseButtonDown(1))
        {
            lightGo.SetActive(true);
        }
    }
}
